'''
Author: LHY 2601958675@qq.com
Date: 2023-05-10 16:48:38
LastEditors: LHY
LastEditTime: 2023-05-11 17:08:52
Description: 
'''
"""
https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C/0/0
抓取豆果美食前三页
    家常菜
    菜名、配料、评分、作者
    保存信息
"""
import requests
from lxml import etree

class all_get(object):
    def __init__(self):
        self.url = 'https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C/0/{}'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
        }
# https://www.douguo.com/caipu/%E5%AE%B6%E5%B8%B8%E8%8F%9C/0/0
    #循环抓取网页
    def My_While(self):
        for i in range(0,3):
            url=self.url.format(i*20)
            self.jxsj_jx(self.ydm_get(url))
    #获取网页源代码
    def ydm_get(self,url):
        response=requests.get(url,headers=self.headers)
        if response.status_code == 200:
            print(response.text)
            return response.text
        else:
            return None

    #解析数据
    def jxsj_jx(self,resp):
        html=etree.HTML(resp)
        data_li=html.xpath('//ul[@class="cook-list"]//li[@class="clearfix"]')
        for data in data_li:
            dict_data={}
            dict_data['菜名']=data.xpath('./a/@title')[0]
            dict_data['菜谱']=data.xpath('./div[@class="cook-info"]/p/text()')
            dict_data['评分']=data.xpath('./div[@class="cook-info"]/div[@class="score"]/span[2]/text()')
            dict_sj4=data.xpath('./div[@class="cook-info"]/div[@class="aut-num clearfix text-lips "]/a/text()')
            sj=str(dict_sj4[1])
            sj2=sj.rstrip()
            dict_data['作者']=sj2
            #保存数据
            for i in dict_data.items():
                a,b=i
                name=f'{a}:{b}\t'
                with open('菜谱.txt','a',encoding='utf-8') as f:
                    f.write(name)
            #换行作用
            na='\n\n'
            with open('菜谱.txt','a',encoding='utf-8') as f:
                f.write(na)


if __name__ == '__main__':
    a=all_get()
    a.My_While()

